/**
 * 
 */
package com.ntv.presentation.spring.web.user;

import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.ntv.domain.IUser;
import com.ntv.domain.User;
import com.ntv.service.DomainException;
import com.ntv.service.user.IUserAppService;

/**
 * @author Vedamoorthi Vairam
 * 
 */
@Controller
@RequestMapping("/signup.html")
@SessionAttributes("userSession")
public class SignupController {

	private final static Logger logger = Logger.getLogger(SignupController.class);
	
	@Autowired
	private IUserAppService userAppService;

	@Autowired
	private Validator validator;
	
	@RequestMapping(method = RequestMethod.GET)
	public void form(Model model) {
		model.addAttribute(MA_USER, new User());
	}
	
	public void setUserAppService(IUserAppService userAppService) {
		this.userAppService = userAppService;
	}

	@Autowired
	SimpleMailMessage mailMessage = null;
	
	@Autowired
	MailSender mailSender = null;

	private static final String MA_USER = "user";

	@RequestMapping(method = RequestMethod.POST)
	public String form(@ModelAttribute(MA_USER) User user, BindingResult result, ModelMap model) {
		verifyBinding(result);
		validator.validate(user, result);
		if (result.hasErrors()) {
			result.reject("form.problems");
			return "signup";
		}

		try
		{
			IUser newUser = userAppService.createUser(user);
			mailMessage.setSentDate(new Date());
			mailMessage.setText("Blah blah blah...");
			// mailSender.send(mailMessage);
			logger.info("New USer id " + newUser.getId());
			model.addAttribute("userSession", newUser);
			return "redirect:/home.htm";
		}
		catch (DomainException e)
		{
			logger.error(e);
			e.printStackTrace();
			return "signup";
		}
	}
		
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		logger.info(" initBinder ");
		binder.setAllowedFields(new String[] { "fname", "lname", "email", "password", "passwordConfirmation" });
	}

	private void verifyBinding(BindingResult result) {
		logger.info(" verifyBinding ");
		String[] suppressedFields = result.getSuppressedFields();
		/*if (suppressedFields.length > 0) {
			throw new RuntimeException(
					"You've attempted to bind fields	[CA]that haven't been allowed in initBinder(): ");
		}*/
	}
	
}
